<template>
  <div class="collection">
    <div class="gva-search-box">
      <div class="gva-btn-list">
        <el-button icon="plus" size="small" type="primary" @click="openDialog('addApi')">新增</el-button>
        <el-popover v-model="deleteVisible" placement="top" width="160">
          <p>确定要删除吗？</p>
          <div style="text-align: right; margin-top: 8px;">
            <el-button link size="small" type="primary" @click="deleteVisible = false">取消</el-button>
            <el-button size="small" type="primary" @click="onDelete">确定</el-button>
          </div>
          <template #reference>
            <el-button
              :disabled="!deleteIds.length"
              icon="delete"
              size="small"
              style="margin-left: 10px;"
              @click="deleteVisible = true"
            >删除
            </el-button>
          </template>
        </el-popover>
      </div>

      <el-form ref="searchForm" :inline="true" :model="searchInfo">
        <el-form-item label="标题">
          <el-input v-model="searchInfo.title" placeholder="标题" />
        </el-form-item>
        <el-form-item label="藏品类型">
          <el-select v-model="searchInfo.type" clearable placeholder="请选择" @change="checkTypeChange">
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="子类型">
          <el-select v-model="searchInfo.subType" clearable placeholder="请选择">
            <el-option
              v-for="item in subTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="search" size="small" type="primary" @click="onSubmit">查询</el-button>
          <el-button icon="refresh" size="small" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
      <el-table :data="tableData" @selection-change="handleSelectionChange">
        <el-table-column
          :selectable="selectStatus"
          type="selection"
          width="55"
        />
        <el-table-column align="left" label="id" min-width="60" prop="id" />
        <el-table-column align="left" label="标题" min-width="60" prop="title" />
        <el-table-column v-slot="scope" align="left" label="藏品类型" min-width="60">
          <span v-if="scope.row.type === 10">模型</span>
          <span v-if="scope.row.type === 20">空间</span>
          <span v-if="scope.row.type === 30">图片</span>
        </el-table-column>
        <el-table-column v-slot="scope" align="left" label="子类型" min-width="60">
          <span v-if="scope.row.subType === 0"> 无 </span>
          <span v-if="scope.row.subType === 1"> 模型类型：OBJ </span>
          <span v-if="scope.row.subType === 2"> 模型类型：GLB </span>
        </el-table-column>
        <el-table-column v-slot="scope" align="left" label="状态" min-width="60">
          <el-button v-if="scope.row.status === 1" type="primary" @click="onChangeStatus(scope.row)">关闭
          </el-button>
          <el-button v-if="scope.row.status === 0" type="primary" @click="onChangeStatus(scope.row)">开启
          </el-button>
        </el-table-column>
        <el-table-column v-slot="scope" label="操作">
          <router-link
            :to="{ path: '/layout/collection/collectionFileList', query: { collectionId: scope.row.id } }"
          >
            <el-button
              :disabled="scope.row.status === 1"
              icon="setting"
              link
              size="small"
              type="primary"
            >设置藏品文件
            </el-button>
          </router-link>
          <el-button
            :disabled="scope.row.status === 1"
            icon="edit"
            link
            size="small"
            type="primary"
            @click="editCollectionFunc(scope.row.id)"
          >编辑
          </el-button>
          <el-button
            :disabled="scope.row.status === 1"
            icon="delete"
            link
            size="small"
            type="primary"
            @click="deleteCollectionFunc(scope.row.id)"
          >删除
          </el-button>
        </el-table-column>
      </el-table>
    </div>
    <div class="gva-pagination">
      <el-pagination
        :current-page="page"
        :page-size="pageSize"
        :page-sizes="[10, 30, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>

    <el-dialog v-if="dialogFormVisible" v-model="dialogFormVisible" :before-close="closeDialog" :title="dialogTitle">
      <edit-collection :detail="detail" @editRes="editRes" />
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Collection',
}
</script>

<script setup>

import { ref } from 'vue'
import { changeStatus, collectionDetail, collectionList, deleteCollection, deleteCollectionByIds } from '@/api/collection'
import { ElMessageBox, ElMessage } from 'element-plus'
import EditCollection from '@/view/collection/edit.vue'

const searchInfo = ref({})
const tableData = ref([])
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
const deleteIds = ref([])
const detail = ref({})

const typeList = [
  {
    'label': '模型',
    'value': 10,
  },
  {
    'label': '空间',
    'value': 20,
  },
  {
    'label': '图片',
    'value': 30,
  },
]

const subTypeList = ref([])

const getTableData = async() => {
  const res = await collectionList({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
  if (res.code === 200) {
    tableData.value = res.data.list
    total.value = res.data.total
    page.value = res.data.page
    pageSize.value = res.data.pageSize
  }
}

const closeDialog = () => {
  detail.value = {}
  dialogFormVisible.value = false
}

const handleSelectionChange = (val) => {
  deleteIds.value = val.map((item) => item.id)
}

const onSubmit = () => {
  page.value = 1
  pageSize.value = 10
  getTableData()
}

const onReset = () => {
  searchInfo.value = {}
}

const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

const dialogTitle = ref('新增藏品')
const dialogFormVisible = ref(false)

const openDialog = () => {
  dialogFormVisible.value = true
}

// 批量删除
const onDelete = async() => {
  if (deleteIds.value.length === 0) {
    ElMessage({
      type: 'warning',
      message: '请选择要删除的数据！'
    })
    return
  }

  const res = await deleteCollectionByIds({ ids: deleteIds.value })
  if (res.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功！'
    })

    if (tableData.value.length === deleteIds.length && page.value > 1) {
      page.value--
    }
    await getTableData()
  }
}

// 编辑
const editRes = (status) => {
  // 如果是添加，则刷一下列表数据
  if (detail.value.id === undefined) {
    page.value = 1
    getTableData()
  }
  detail.value = {}
  dialogFormVisible.value = status
}

// 编辑
const editCollectionFunc = async(id) => {
  // 查询详情
  const res = await collectionDetail({ id: id })
  if (res.code === 200) {
    dialogTitle.value = '编辑藏品'
    detail.value = res.data
  }

  dialogFormVisible.value = true
}

// 删除
const deleteCollectionFunc = async(id) => {
  ElMessageBox.confirm('此操作将永久删除该藏品, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async() => {
    const res = await deleteCollection({ id: id })
    if (res.code === 200) {
      ElMessage({
        type: 'success',
        message: '删除成功!'
      })
      if (tableData.value.length === 1 && page.value > 1) {
        page.value--
      }
      await getTableData()
    }
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除'
    })
  })
}

const checkTypeChange = () => {
  if (searchInfo.value.type === 10) {
    subTypeList.value = [
      {
        'label': '模型类型:OBJ',
        'value': 1,
      },
      {
        'label': '模型类型:GLB',
        'value': 2,
      },
    ]
  } else {
    searchInfo.value.subType = undefined
    subTypeList.value = []
  }
}

// 更新状态
const onChangeStatus = async(val) => {
  let status = 0
  let message = '已关闭'
  if (val.status === 0) {
    status = 1
    message = '已开启'
  }

  const res = await changeStatus({
    id: val.id,
    status: status
  })
  if (res.code === 200) {
    ElMessage({
      type: 'success',
      message: message,
    })
    val.status = status
  }
}

const selectStatus = (row) => {
  return row.status !== 1
}

getTableData()

</script>
